import time
import hmac
import hashlib
import base64
import urllib.parse

def generate_dingtalk_sign(secret):
    timestamp = str(round(time.time() * 1000))
    secret_enc = secret.encode('utf-8')
    string_to_sign = '{}\n{}'.format(timestamp, secret)
    string_to_sign_enc = string_to_sign.encode('utf-8')
    hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest()
    sign = urllib.parse.quote_plus(base64.b64encode(hmac_code).decode('utf-8'))
    return timestamp, sign

if __name__ == "__main__":
    secret = 'SEC73025ebc77833a0d097fcda3c0d18c255cfc70425b12f3ce399c4ecd845c8c93'  # 替换为你的密钥
    timestamp, sign = generate_dingtalk_sign(secret)
    with open("sign_data.txt", "w") as f:
        f.write(f"{timestamp} {sign}")
